﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Songhay.Extensions;
using System;
using System.IO;
using System.Linq;

namespace Songhay.BiggestBox.Desktop.Tests
{
    [TestClass]
    public class CodePlexTest
    {
        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize]
        public void InitializeTest()
        {
            this.TestContext.RemovePreviousTestResults();
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }
        [TestMethod]
        [TestProperty("slnFile", @"E:\~shares\dataRoot\Visual Studio 2012\Projects\Songhay.BiggestBox.sln")]
        public void ShouldGenerateRsyncCommands()
        {
            var template = @"
ECHO Synching {0} project...
""%ProgramFiles%\cwRsync\bin\rsync"" ^
    --chmod=ugo=rwX ^
    --delete-after ^
    --exclude 'bin/' ^
    --exclude 'obj/' ^
    --exclude '*.csproj.user' ^
    --exclude '*.csproj.vspscc' ^
    --no-perms ^
    --omit-dir-times ^
    --recursive ^
    --stats ^
    --times ^
    '/cygdrive/e/~shares/dataRoot/Visual Studio 2012/Projects/{0}/' ^
    '/cygdrive/e/~shares/dataRoot/Visual Studio 2012/Projects (Public)/codeplex_workspace/wpfbiggestbox/{0}/'
";
            var slnFile = this.TestContext.Properties["slnFile"].ToString();

            Func<string, bool> startsWithProject = i => i.StartsWith("Project(");
            Func<string, bool> containsModulesWithPeriod = i => i.Contains("Modules.");

            var lines = File.ReadAllText(slnFile)
                .Split(Environment.NewLine.ToArray())
                .Where(startsWithProject.And(containsModulesWithPeriod))
                .ToList();

            Assert.IsTrue(lines.Any());

            var output = string.Empty;
            lines.ForEach(i =>
            {
                var name = i.Split('"').Where(containsModulesWithPeriod).First();
                output += string.Format(template, name);
            });

            this.TestContext.WriteLine(output);
        }
    }
}
